<?php
class KolSys_Loader
{
	public static function loadClass($class, $dirs = null)
	{
		$classAlreadyLoaded=(class_exists($class, false) || interface_exists($class, false));
		if($classAlreadyLoaded){return;}
		$badDirArgument=(($dirs !== null) && !is_array($dirs) && !is_string($dirs));
		if($badDirArgument)
		{
			require_once 'KolSys/Exception.php';
			throw new KolSys_Exception('Directory argument must be a string or an array.');
		}

		$classFileToLoad = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';

		if(!empty($dirs))
		{
			$dirPath = dirname($classFileToLoad);
			if(is_string($dirs))
			{
				$dirs = explode(PATH_SEPARATOR, $dirs);
			}
			foreach ($dirs as $key => $dir)
			{
				if ($dir == '.')
				{
					$dirs[$key] = $dirPath;
				} else {
					$dir = rtrim($dir, '\\/');
					$dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath;
				}
			}//foreach
			$classFileToLoad = basename($classFileToLoad);
			self::loadFile($classFileToLoad, $dirs, true);
		} else {
			self::_securityCheck($classFileToLoad);
			include_once $classFileToLoad;
		}
		if (!class_exists($class, false) && !interface_exists($class, false))
		{
			require_once 'KolSys/Exception.php';
			throw new KolSys_Exception("File \"$classFileToLoad\" does not exist or class \"$class\" was not found in the file");
		}
    }//method

    public static function isReadable($filename)
    {
        if (!$fh = @fopen($filename, 'r', true)) {
            return false;
        }
        @fclose($fh);
        return true;
    }

    public static function autoload($class)
    {
        try {
            self::loadClass($class);
            return $class;
        } catch (Exception $e) {
            return false;
        }
    }

    public static function registerAutoload($class = 'KolSys_Loader', $enabled = true)
    {
        if (!function_exists('spl_autoload_register')) {
            require_once 'KolSys/Exception.php';
            throw new KolSys_Exception('spl_autoload does not exist in this PHP installation');
        }

        self::loadClass($class);
        $methods = get_class_methods($class);
        if (!in_array('autoload', (array) $methods)) {
            require_once 'KolSys/Exception.php';
            throw new KolSys_Exception("The class \"$class\" does not have an autoload() method");
        }

        if ($enabled === true) {
            spl_autoload_register(array($class, 'autoload'));
        } else {
            spl_autoload_unregister(array($class, 'autoload'));
        }
    }

    protected static function _securityCheck($filename)
    {
        if (preg_match('/[^a-z0-9\\/\\\\_.-]/i', $filename)) {
            require_once 'KolSys/Exception.php';
            throw new KolSys_Exception('Security check: Illegal character in filename');
        }
    }

    protected static function _includeFile($filespec, $once = false)
    {
        if ($once) {
            return include_once $filespec;
        } else {
            return include $filespec ;
        }
    }
}//class